package esercizi_fondamenti;

import java.util.Scanner;

public class Marzo2004 {
	
	static Scanner in=new Scanner(System.in);

	public static int[] creaVettoreSegmentiOR(int[][]m){
		if(m.length!=m[0].length) 
			System.exit(-1);
		
		
		int[]ris=new int[m.length];
		int pointer=0;
		for(int i=0; i<m.length; i++){
			int somma=0;
			for(int j=pointer; j<m.length; j++){
				somma+=m[i][j];
			}
				ris[pointer]=somma;
				pointer++;
		}
		return ris;
		}//creaVettoreSegmentiOR
	
	public static void creaVettoreTI(int[][]m){
		if(m.length!=m[0].length) 
			System.exit(-1);
		
		int t=0;
		for(int i=0; i<m.length; i=i+2){
			for(int j=0; j<t; j++){
				if(m[i][j]%2==0) 
					System.out.print(m[i][j]+" ");
			}
			t+=2;
		}
		
	}//creaVettoreTI
	
	public static void annullaRigheDispari(int[][]m){
		if(m.length!=m[0].length) 
			System.exit(-1);
	
		
		for(int i=0; i<m.length; i++){
			for(int j=0; j<m.length; j++){
			if(i%2==1) m[i][j]=0;
		}
			}
	}//annullaRigheDispari
	
	public static int[] filtraVettore(int[]v){
//ris contiene el di v che sono positivi e sono in posizione dispari.
		int size=2;
		for(int i=1; i<v.length; i=i+2){
			
		}
		int[] ris=new int[size];
		for(int i=1; i<v.length; i=i+2){
			int cont=0;
				if(v[i]>0) {
				ris[cont]=v[i];
				cont++;
			}
		}
			
		return ris;
	}//filtraVettore
	
	public static int[] creaVettoreDP(int[][]m){
		if(m.length!=m[0].length) 
			System.exit(-1);
		
		int somma=0;
		for(int i=0; i<m.length; i++){
				somma=somma+m[i][i];
		}
		double media=somma/m.length;
		int size=0;
		for(int i=0; i<m.length; i++){
			if(m[i][i]>media) size++;
		}
		int[] ris=new int[size];
		int cont=0;
		for(int i=0; i<m.length; i++){
			if(m[i][i]>media) {
			ris[cont]=m[i][i];
			cont++;
		}
		}
		return ris;
		}//creaVettoreDP
	
	public static int[] costruisciVettore(int[][]m){
		if(m.length!=m[0].length) 
			System.exit(-1);
		
		int dim=(m.length*m.length-m.length)/2;
		int[] ris=new int[dim];
		int pointer=0;
		int from=1;
		for(int i=0; i<m.length; i++){
			for(int j=from; j<m.length; j++){
				ris[pointer]=m[i][j]; 
				pointer++;
			}
			from++;
		}
		return ris;
	}//costruisciVettore
			
	public static void annullaColonne(int[][]m, int h, int k){
		if(m.length!=m[0].length) 
			System.exit(-1);
		//h<k
		for(int i=0; i<m.length; i++){
			for(int j=h; j<=k; j++){
				
					m[i][j]=0;
			}
				
			}
		
	}
			
	public static void main(String[] args) {
		System.out.println("filtra vettore");
		int[]vettore={13,-11,4,7,-17,5};
		int[]risu=filtraVettore(vettore);
		for(int j=0; j<risu.length; j++){
			System.out.print(risu[j]+" ");
		}
		System.out.println();
		
		System.out.println("creavettoreDP");
		int[][]mat={{6,2,11,9,12},{8,14,10,3,1},{14,21,11,2,2},{8,1,13,1,7},{10,17,13,10,3}};
		int[]vv=creaVettoreDP(mat);
		for(int i=0; i<vv.length; i++){
			System.out.print(vv[i]+ " ");
			}	
			System.out.println();
			System.out.println();
			System.out.println("costruiscivettore");
		int[]diag=costruisciVettore(mat);
			for(int j=0; j<diag.length; j++){
				System.out.print(diag[j]+" ");
			}
			System.out.println();
			System.out.println("annulla colonne");
			System.out.println("inserire due valori h e k con h<k");
			int h=in.nextInt();
			int k=in.nextInt();
			annullaColonne(mat, h, k);
			for(int i=0; i<mat.length; i++){
				for(int j=0; j<mat.length; j++){
					System.out.print(mat[i][j]+" ");
				}	
				System.out.println();
			}
		
		
		System.out.println("altra matrice");
		int[][]m={{6,2,11,9,12},{8,13,10,3,1},{14,21,11,2,2},{8,1,13,1,7},{10,17,12,10,3}};
		int[]ris=creaVettoreSegmentiOR(m);
		for(int i=0; i<ris.length; i++){
			System.out.print(ris[i]+" ");
		}
		System.out.println();
		System.out.println();
		creaVettoreTI(m);
		System.out.println();
		System.out.println();
		System.out.println("annulla righe dispari");
		annullaRigheDispari(m);
		for(int i=0; i<m.length; i++){
			for(int j=0; j<m.length; j++){
				System.out.print(m[i][j]+" ");
			}	
			System.out.println();
		}
	}

}
